package com.gph.saviorframework.dfs.controller;

import com.alibaba.fastjson.JSONArray;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.SaviorFrameworkException;
import com.gph.saviorframework.common.model.Item;
import com.gph.saviorframework.config.service.ItemService;
import com.gph.saviorframework.config.service.SubItemService;
import com.gph.saviorframework.fastdfs.monitor.model.Group;
import com.gph.saviorframework.fastdfs.monitor.model.Storage;
import com.gph.saviorframework.fastdfs.monitor.service.MonitorService;
import com.gph.saviorframework.util.RequestUtil;
import com.gph.saviorframework.util.SecurityUtils;
import com.jcraft.jsch.JSchException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/" + Constants.FRAMEWORK_REQUEST_PATH + "/dfs")
public class MonitorController {

    @Autowired
    private MonitorService monitorService;

    @RequestMapping(value = "/monitor/performance", method = RequestMethod.GET)
    public ModelAndView performancePage() throws SaviorFrameworkException, IOException, JSchException {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("sf/dfs/monitor/performance");
        mv.addObject("groups",monitorService.listGroupInfo());
        return mv;
    }

    @RequestMapping(value = "/monitor/capacity", method = RequestMethod.GET)
    public ModelAndView capaticyPage() throws SaviorFrameworkException, IOException, JSchException {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("sf/dfs/monitor/capacity");
        mv.addObject("groups",monitorService.listGroupInfo());
        return mv;
    }

    @RequestMapping(value = "/monitor/performance", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap performance() throws SaviorFrameworkException, IOException, JSchException {
        ModelMap modelMap = new ModelMap();
        try {
            //只返回需要的数据
            List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();

            List<Group> groups = monitorService.listGroupInfo();
            for(Group group : groups){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("groupName",group.getGroupName());
                //添加storage的信息
                List<Map<String,Object>> storageList = new ArrayList<Map<String, Object>>();
                for(Storage storage : group.getStorageList()){
                    Map<String,Object> storageMap = new HashMap<String, Object>();
                    storageMap.put("ipAddr",storage.getIpAddr());
                    /*storageMap.put("cpu",storage.getCpu());
                    storageMap.put("mem",storage.getMem());*/

                    storageList.add(storageMap);
                }
                map.put("storages",storageList);
                list.add(map);
            }
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
            modelMap.addAttribute("groups",list);
        }
        catch (Exception ex){
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.FALSE);
        }
        return modelMap;
    }
}
